<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Home\Controller ;

/**
 * Description of UserController
 *
 * @author mooshroom
 */
class UserController extends \Think\Controller
{

    //put your code here
    protected $UserLoginModel            = null ;
    protected $StudentBaseModel          = null ;
    protected $ActivityDictionaryModel   = null ;
    protected $ActivityLinkModel         = null ;
    protected $AcademyDictionaryModel    = null ;
    protected $ProfessionDictionaryModel = null ;

    function __construct ()
    {
        parent::__construct () ;
        $this->UserLoginModel            = new \Home\Model\UserLoginModel() ;
        $this->StudentBaseModel          = new \Home\Model\StudentBaseModel() ;
        $this->ActivityDictionaryModel   = new \Home\Model\ActivityDictionaryModel() ;
        $this->ActivityLinkModel         = new \Home\Model\UserActivityLinkModel() ;
        $this->AcademyDictionaryModel    = new \Home\Model\AcademyDictionaryModel() ;
        $this->ProfessionDictionaryModel = new \Home\Model\ProfessionDictionaryModel() ;
    }

    //修改学生基本信息页面
    function student_info_update ()
    {
        if ( session ( 'UID' ) )
        {
            $array      = $this->StudentBaseModel->getStudentBaseInforByUID ( session ( 'UID' ) ) ;
            $this->assign ( $array ) ;
            $Academy    = $this->AcademyDictionaryModel->select () ;
            $this->assign ( 'Academy' , $Academy ) ;
            $Profession = $this->ProfessionDictionaryModel->select () ;
            $this->assign ( 'Profession' , $Profession ) ;
            $Grade      = D ( 'GradeDictionary' )->select () ;
            $this->assign ( 'Grade' , $Grade ) ;
            $Class      = D ( 'ClassDictionary' )->select () ;
            $this->assign ( 'Class' , $Class ) ;
            $this->display () ;
        }
        else
        {
            $this->error ( '登录超时' ) ;
        }
    }

    //修改密码界面
    function change_password ()
    {
        if ( session ( 'UID' ) )
        {
            $this->display () ;
        }
        else
        {
            $this->error ( '登录超时' ) ;
        }
    }

    //打印活动记录
    function print_activity ()
    {
        $ActivityLink            = M () ;
        $getActivityListSql      = 'SELECT * FROM ' . C ( 'DB_PREFIX' ) . 'activity_dictionary A,' . C ( 'DB_PREFIX' ) . 'user_activity_link B where B.UID="' . session ( 'UID' ) . '" AND A.ActivityID=B.ActivityID' ;
        $Student                 = $this->StudentBaseModel->getStudentBaseInforByUID ( session ( 'UID' ) ) ;
        $Student[ 'Academy' ]    = $this->AcademyDictionaryModel->getAcademyNameByAcademyID ( $Student[ 'AcademyID' ] ) ;
        $Student[ 'Profession' ] = $this->ProfessionDictionaryModel->getProfessionNameByProfessionID ( $Student[ 'ProfessionID' ] ) ;

        $Activities = $ActivityLink->query ( $getActivityListSql ) ;
//        dump ( $Student ) ;
        if ( false != $Activities )
        {
            $Empty = array () ;
            //累计时数计算
            foreach ( $Activities as
                    $k =>
                    $v )
            {
                $Activities[ $k ][ 'SumHours' ] = isset ( $$Activities[ $k ][ 'SumHours' ] ) ? $Activities[ $k ][ 'SumHours' ] + $Activities[ $k ][ 'ActivityHours' ] : $Activities[ $k ][ 'ActivityHours' ] ;
            }
//            生成空记录
            foreach ( $Activities[ 0 ] as
                    $k =>
                    $v )
            {
                $Empty[ $k ] = '' ;
            }
            //记录补全
            $count = count ( $Activities ) ;
            for ( $i = 0 ;
                    $i < (8 - $count ) ;
                    $i ++ )
            {
                $Activities[ $count + $i ] = $Empty ;
            }
        }
//        dump ( $Activities ) ;
        $this->assign ( 'Student' , $Student ) ;
        $this->assign ( 'Activities' , $Activities ) ;
        $this->display ( 'table' ) ;
    }

    //显示用户基本信息页面
    function student_info ()
    {
        if ( session ( 'UID' ) )
        {
            $Student                 = $this->StudentBaseModel->getStudentBaseInforByUID ( session ( 'UID' ) ) ;
            //获取学院名称
            $Student[ 'Academy' ]    = $this->AcademyDictionaryModel
                    ->getAcademyNameByAcademyID ( $array[ 'AcademyID' ] ) ;
            //获取专业名称
            $Student[ 'Profession' ] = $this->ProfessionDictionaryModel
                    ->getProfessionNameByProfessionID ( $array[ 'ProfessionID' ] ) ;
            //获取班级信息
//            $Student['Class']
            $this->assign ( $Student ) ;
            //获取用户性别
            $UIDCardNumber           = $Student[ 'UIDCardNumber' ] ;
            $USex                    = substr ( $UIDCardNumber , 16 , 1 ) % 2 ;
            if ( $USex )
            {
                $this->assign ( 'Sex' , '男' ) ;
            }
            else
            {
                $this->assign ( 'Sex' , '女' ) ;
            }
            //assign学院名称
            $this->assign ( 'AcademyID' , $Student[ 'AcademyID' ] ) ;
            //assign专业
            $this->assign ( 'ProfessionID' , $Student[ 'ProfessionID' ] ) ;
            //assign班级
            $this->assign ( 'ClassID' , $Student[ 'ClassID' ] ) ;

            $this->display () ;
//            dump ( $Student ) ;
        }
        else
        {
            $this->error ( '登录超时' ) ;
        }
    }

    function uname ()
    {
        $model   = M () ;
        $Student = $model->query ( 'select * from work_temp' ) ;
        foreach ( $Student as
                $v )
        {
            dump ( $model->execute ( 'update work_student_base set UName="' . $v[ 'UName' ] . '" where UID="' . $v[ 'UID' ] . '"' ) ) ;
        }
    }

    function print_all ()
    {
        $model = M () ;
        $rs    = $model->query ( 'select * from work_temp' ) ;
        echo '<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>' ;
        foreach ( $rs as
                $v )
        {
            $html+=$this->print_all_sub ( $v[ 'UID' ] ) ;
        }
        echo '<body/><html/>' ;
//        $this->show ( $html ) ;
    }

    function print_all_sub ( $UID )
    {
        $ActivityLink            = M () ;
        $getActivityListSql      = 'SELECT * FROM ' . C ( 'DB_PREFIX' ) . 'activity_dictionary A,' . C ( 'DB_PREFIX' ) . 'user_activity_link B where B.UID="' . $UID . '" AND A.ActivityID=B.ActivityID' ;
        $Student                 = $this->StudentBaseModel->getStudentBaseInforByUID ( $UID ) ;
        $Student[ 'Academy' ]    = $this->AcademyDictionaryModel->getAcademyNameByAcademyID ( $Student[ 'AcademyID' ] ) ;
        $Student[ 'Profession' ] = $this->ProfessionDictionaryModel->getProfessionNameByProfessionID ( $Student[ 'ProfessionID' ] ) ;
        $Activities              = $ActivityLink->query ( $getActivityListSql ) ;
        if ( false != $Activities )
        {
            $Empty = array () ;
            //累计时数计算
            foreach ( $Activities as
                    $k =>
                    $v )
            {
                $Activities[ $k ][ 'SumHours' ] = isset ( $$Activities[ $k ][ 'SumHours' ] ) ? $Activities[ $k ][ 'SumHours' ] + $Activities[ $k ][ 'ActivityHours' ] : $Activities[ $k ][ 'ActivityHours' ] ;
            }
//            生成空记录
            foreach ( $Activities[ 0 ] as
                    $k =>
                    $v )
            {
                $Empty[ $k ] = '' ;
            }
            //记录补全
            $count = count ( $Activities ) ;
            for ( $i = 0 ;
                    $i < (8 - $count ) ;
                    $i ++ )
            {
                $Activities[ $count + $i ] = $Empty ;
            }
        }
        $this->assign ( 'Student' , $Student ) ;
        $this->assign ( 'Activities' , $Activities ) ;
        $this->display ( 'table_all.min' ) ;
//        $this->display () ;
    }

}
